﻿package mortal.game.scene3D.display3d.text3d.dynamicText3d
{
    import baseEngine.core.*;
    import com.gengine.debug.*;
    import flash.display.*;
    import flash.display3D.textures.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import frEngine.render.layer.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.display3d.text3d.*;

    public class Text3DBigImg extends Object
    {
        private var htmlMap:Array;
        private var _pool:Array;
        public var textMesh3d:Text3DMesh;
        private var _lastDisposeTime:Number = 0;
        private var _disposeTime:Number = 120000;
        private var _hasCreateMaxNum:int = 0;
        private var placeList:Array;
        public var curY:int = 0;
        private var _bmpdata:BitmapData;
        public var needUpload:Boolean = false;
        private static const _m:Matrix = new Matrix();

        public function Text3DBigImg(param1:int)
        {
            this.htmlMap = [];
            this._pool = new Array();
            this.placeList = [];
            this._bmpdata = new BitmapData(param1, 256, true, 0);
            this.textMesh3d = new Text3DMesh(this._bmpdata, false);
            this.textMesh3d.setLayer(Layer3DManager.text3DLayer, false);
            this.textMesh3d.render = Text3DRender.instance;
            return;
        }// end function

        public function canDispose(param1:Number) : Boolean
        {
            if (param1 - this._lastDisposeTime > this._disposeTime && this.placeList.length == this._hasCreateMaxNum && this.curY > 200)
            {
                return true;
            }
            return false;
        }// end function

        public function isFull(param1:int) : Boolean
        {
            return this.curY + param1 + 4 > 256;
        }// end function

        public function init(param1:GameScene3D) : void
        {
            param1.addChild(this.textMesh3d);
            return;
        }// end function

        public function reinit(param1:GameScene3D) : void
        {
            this.textMesh3d.textBg0.reUpload();
            return;
        }// end function

        public function uploadTexture(param1:GameScene3D) : void
        {
            var scene:* = param1;
            if (this.textMesh3d.textBg0.texture)
            {
                try
                {
                    Texture(this.textMesh3d.textBg0.texture).uploadFromBitmapData(this._bmpdata, 0);
                    this.needUpload = false;
                }
                catch (e:Error)
                {
                    Log.error("upload text3DBigImg error:" + e.message);
                    textMesh3d.textBg0.download();
                    needUpload = true;
                }
            }
            else
            {
                this.textMesh3d.textBg0.immediatelyUpload();
                this.needUpload = false;
            }
            return;
        }// end function

        public function createPos(param1:TextField, param2:String) : Text3DInfo
        {
            this.htmlMap.push(param2);
            var _loc_3:* = this.curY;
            var _loc_4:* = int(param1.textWidth / 2 + 2) * 2;
            var _loc_5:* = int(param1.textHeight / 2 + 2) * 2;
            var _loc_6:* = new Text3DInfo(_loc_3, _loc_4, _loc_5, this);
            _m.identity();
            _m.translate(0, _loc_3);
            this._bmpdata.draw(param1, _m);
            this.needUpload = true;
            this.curY = this.curY + _loc_5;
            return _loc_6;
        }// end function

        private function releasePlace(param1:Text3D) : void
        {
            param1.hide();
            this.placeList.push(param1.place);
            return;
        }// end function

        private function getPlace() : Text3DPlace
        {
            if (this.placeList.length > 0)
            {
                return this.placeList.shift();
            }
            var _loc_1:String = this;
            var _loc_2:* = this._hasCreateMaxNum + 1;
            _loc_1._hasCreateMaxNum = _loc_2;
            return this.textMesh3d.createNewPlace();
        }// end function

        public function createText3D(param1:TextField, param2:Text3DInfo, param3:Boolean, param4:Pivot3D, param5:Text3D = null) : Text3D
        {
            var _loc_6:* = param5;
            if (_loc_6)
            {
                if (_loc_6.bigImg != this)
                {
                    _loc_6.bigImg.releasePlace(_loc_6);
                    _loc_6.reInit(this, this.getPlace(), param3, param4);
                }
                else
                {
                    _loc_6.reInit(this, null, param3, param4);
                }
            }
            else
            {
                if (this._pool.length > 0)
                {
                    _loc_6 = this._pool.pop();
                }
                else
                {
                    _loc_6 = new Text3D();
                }
                _loc_6.reInit(this, this.getPlace(), param3, param4);
            }
            _loc_6.setImgPos(param2.offsetY, param2.width, param2.height, param1.text);
            return _loc_6;
        }// end function

        public function disposeText3D(param1:Text3D) : void
        {
            if (this._pool && this._pool.indexOf(param1) == -1)
            {
                this._pool.push(param1);
            }
            param1.hide();
            this.placeList.push(param1.place);
            this._lastDisposeTime = getTimer();
            return;
        }// end function

        public function disposeAll(param1:Dictionary) : void
        {
            var _loc_4:String = null;
            var _loc_5:Text3D = null;
            this.textMesh3d.dispose(false);
            var _loc_2:* = this._pool.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_5 = this._pool[_loc_3];
                _loc_5.clear();
                _loc_3++;
            }
            this.textMesh3d = null;
            this._pool = null;
            this._bmpdata.dispose();
            this._bmpdata = null;
            for each (_loc_4 in this.htmlMap)
            {
                
                delete param1[_loc_4];
            }
            return;
        }// end function

    }
}
